/*
 * @Author: 0x9DEFA478
 * @Date: 2022-06-18 00:15:30
 * @LastEditTime: 2022-06-18 16:46:14
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "IP_BDMA.h"




extern void BDMA_Channel0_SetIrqCallback(void (*IP_IRQ_Callback)(void*),void* v);
extern void BDMA_Channel1_SetIrqCallback(void (*IP_IRQ_Callback)(void*),void* v);
extern void BDMA_Channel2_SetIrqCallback(void (*IP_IRQ_Callback)(void*),void* v);
extern void BDMA_Channel3_SetIrqCallback(void (*IP_IRQ_Callback)(void*),void* v);
extern void BDMA_Channel4_SetIrqCallback(void (*IP_IRQ_Callback)(void*),void* v);
extern void BDMA_Channel5_SetIrqCallback(void (*IP_IRQ_Callback)(void*),void* v);
extern void BDMA_Channel6_SetIrqCallback(void (*IP_IRQ_Callback)(void*),void* v);
extern void BDMA_Channel7_SetIrqCallback(void (*IP_IRQ_Callback)(void*),void* v);




static void IP_BDMA_SetIF(IP_BDMA* ip_bdma){
  void* Instance;

  Instance=ip_bdma->Instance.Instance;

  if(Instance==BDMA_Channel0){
    ip_bdma->TC=BDMA_FLAG_TC0;
    ip_bdma->HT=BDMA_FLAG_TC0;
    ip_bdma->TE=BDMA_FLAG_TC0;
    ip_bdma->GL=BDMA_FLAG_GL0;

    ip_bdma->Instance.ISR=&BDMA->ISR;
    ip_bdma->Instance.IFCR=&BDMA->IFCR;

    ip_bdma->Instance.DMAMUX.CCR=&DMAMUX2_Channel0->CCR;
    ip_bdma->DMAMUX_SOF=0x1U<<0;

    ip_bdma->SetIrqCallback=BDMA_Channel0_SetIrqCallback;
  }else if(Instance==DMA1_Stream1){
    ip_bdma->TC=BDMA_FLAG_TC1;
    ip_bdma->HT=BDMA_FLAG_TC1;
    ip_bdma->TE=BDMA_FLAG_TC1;
    ip_bdma->GL=BDMA_FLAG_GL1;

    ip_bdma->Instance.ISR=&BDMA->ISR;
    ip_bdma->Instance.IFCR=&BDMA->IFCR;

    ip_bdma->Instance.DMAMUX.CCR=&DMAMUX2_Channel1->CCR;
    ip_bdma->DMAMUX_SOF=0x1U<<1;

    ip_bdma->SetIrqCallback=BDMA_Channel1_SetIrqCallback;
  }else if(Instance==DMA1_Stream2){
    ip_bdma->TC=BDMA_FLAG_TC2;
    ip_bdma->HT=BDMA_FLAG_TC2;
    ip_bdma->TE=BDMA_FLAG_TC2;
    ip_bdma->GL=BDMA_FLAG_GL2;

    ip_bdma->Instance.ISR=&BDMA->ISR;
    ip_bdma->Instance.IFCR=&BDMA->IFCR;

    ip_bdma->Instance.DMAMUX.CCR=&DMAMUX2_Channel2->CCR;
    ip_bdma->DMAMUX_SOF=0x1U<<2;

    ip_bdma->SetIrqCallback=BDMA_Channel2_SetIrqCallback;
  }else if(Instance==DMA1_Stream3){
    ip_bdma->TC=BDMA_FLAG_TC3;
    ip_bdma->HT=BDMA_FLAG_TC3;
    ip_bdma->TE=BDMA_FLAG_TC3;
    ip_bdma->GL=BDMA_FLAG_GL3;

    ip_bdma->Instance.ISR=&BDMA->ISR;
    ip_bdma->Instance.IFCR=&BDMA->IFCR;

    ip_bdma->Instance.DMAMUX.CCR=&DMAMUX2_Channel3->CCR;
    ip_bdma->DMAMUX_SOF=0x1U<<3;

    ip_bdma->SetIrqCallback=BDMA_Channel3_SetIrqCallback;
  }else if(Instance==DMA1_Stream4){
    ip_bdma->TC=BDMA_FLAG_TC4;
    ip_bdma->HT=BDMA_FLAG_TC4;
    ip_bdma->TE=BDMA_FLAG_TC4;
    ip_bdma->GL=BDMA_FLAG_GL4;

    ip_bdma->Instance.ISR=&BDMA->ISR;
    ip_bdma->Instance.IFCR=&BDMA->IFCR;

    ip_bdma->Instance.DMAMUX.CCR=&DMAMUX2_Channel4->CCR;
    ip_bdma->DMAMUX_SOF=0x1U<<4;

    ip_bdma->SetIrqCallback=BDMA_Channel4_SetIrqCallback;
  }else if(Instance==DMA1_Stream5){
    ip_bdma->TC=BDMA_FLAG_TC5;
    ip_bdma->HT=BDMA_FLAG_TC5;
    ip_bdma->TE=BDMA_FLAG_TC5;
    ip_bdma->GL=BDMA_FLAG_GL5;

    ip_bdma->Instance.ISR=&BDMA->ISR;
    ip_bdma->Instance.IFCR=&BDMA->IFCR;

    ip_bdma->Instance.DMAMUX.CCR=&DMAMUX2_Channel5->CCR;
    ip_bdma->DMAMUX_SOF=0x1U<<5;

    ip_bdma->SetIrqCallback=BDMA_Channel5_SetIrqCallback;
  }else if(Instance==DMA1_Stream6){
    ip_bdma->TC=BDMA_FLAG_TC6;
    ip_bdma->HT=BDMA_FLAG_TC6;
    ip_bdma->TE=BDMA_FLAG_TC6;
    ip_bdma->GL=BDMA_FLAG_GL6;

    ip_bdma->Instance.ISR=&BDMA->ISR;
    ip_bdma->Instance.IFCR=&BDMA->IFCR;

    ip_bdma->Instance.DMAMUX.CCR=&DMAMUX2_Channel6->CCR;
    ip_bdma->DMAMUX_SOF=0x1U<<6;

    ip_bdma->SetIrqCallback=BDMA_Channel6_SetIrqCallback;
  }else if(Instance==DMA1_Stream7){
    ip_bdma->TC=BDMA_FLAG_TC7;
    ip_bdma->HT=BDMA_FLAG_TC7;
    ip_bdma->TE=BDMA_FLAG_TC7;
    ip_bdma->GL=BDMA_FLAG_GL7;

    ip_bdma->Instance.ISR=&BDMA->ISR;
    ip_bdma->Instance.IFCR=&BDMA->IFCR;

    ip_bdma->Instance.DMAMUX.CCR=&DMAMUX2_Channel7->CCR;
    ip_bdma->DMAMUX_SOF=0x1U<<7;

    ip_bdma->SetIrqCallback=BDMA_Channel7_SetIrqCallback;
  }else{
    for(;;){
    }
  }
  
  ip_bdma->Instance.DMAMUX.CSR=&DMAMUX2_ChannelStatus->CSR;
  ip_bdma->Instance.DMAMUX.CFR=&DMAMUX2_ChannelStatus->CFR;

  ip_bdma->Instance.DMAMUX_RG.RGCR=((void*)0);
  ip_bdma->Instance.DMAMUX_RG.RGSR=((void*)0);
  ip_bdma->Instance.DMAMUX_RG.RGCFR=((void*)0);

  ip_bdma->ALL=ip_bdma->TC|ip_bdma->HT|ip_bdma->TE|ip_bdma->GL;

}




/**
 * @brief 设置BDMA请求
 * @param ip_bdma IP_BDMA对象
 * @param Req 请求代号(通道号)
 * @return 无
 */
void IP_BDMA_SetReq(IP_BDMA* ip_bdma,unsigned int Req){

  ip_bdma->Instance.DMAMUX.CCR[0]=Req&DMAMUX_CxCR_DMAREQ_ID;

  if((Req>=BDMA_REQUEST_GENERATOR0)&&(Req<=BDMA_REQUEST_GENERATOR7)){
    switch(Req){
      case BDMA_REQUEST_GENERATOR0:
        ip_bdma->Instance.DMAMUX_RG.RGCR=&DMAMUX2_RequestGenerator0->RGCR;
        break;
      case BDMA_REQUEST_GENERATOR1:
        ip_bdma->Instance.DMAMUX_RG.RGCR=&DMAMUX2_RequestGenerator1->RGCR;
        break;
      case BDMA_REQUEST_GENERATOR2:
        ip_bdma->Instance.DMAMUX_RG.RGCR=&DMAMUX2_RequestGenerator2->RGCR;
        break;
      case BDMA_REQUEST_GENERATOR3:
        ip_bdma->Instance.DMAMUX_RG.RGCR=&DMAMUX2_RequestGenerator3->RGCR;
        break;
      case BDMA_REQUEST_GENERATOR4:
        ip_bdma->Instance.DMAMUX_RG.RGCR=&DMAMUX2_RequestGenerator4->RGCR;
        break;
      case BDMA_REQUEST_GENERATOR5:
        ip_bdma->Instance.DMAMUX_RG.RGCR=&DMAMUX2_RequestGenerator5->RGCR;
        break;
      case BDMA_REQUEST_GENERATOR6:
        ip_bdma->Instance.DMAMUX_RG.RGCR=&DMAMUX2_RequestGenerator6->RGCR;
        break;
      case BDMA_REQUEST_GENERATOR7:
        ip_bdma->Instance.DMAMUX_RG.RGCR=&DMAMUX2_RequestGenerator7->RGCR;
        break;
      default:
        for(;;){
        }
    }

    ip_bdma->Instance.DMAMUX_RG.RGSR=&DMAMUX2_RequestGenStatus->RGSR;
    ip_bdma->Instance.DMAMUX_RG.RGCFR=&DMAMUX2_RequestGenStatus->RGCFR;
  }
}

/**
 * @brief 关闭BDMA的所有中断
 * @param ip_bdma IP_BDMA对象
 * @return 0:成功 其他:不支持的操作
 */
int IP_BDMA_DisableAll_IT(IP_BDMA* ip_bdma){

  ip_bdma->Instance.Instance->CR&=(~(BDMA_CCR_TCIE|BDMA_CCR_HTIE|BDMA_CCR_TEIE));//关闭中断

  return 0;
}

/**
 * @brief 初始化 IP_BDMA
 * @param ip_bdma 空的IP_BDMA对象(已分配内存的)
 * @param _bdma bdma句柄
 * @return 0:成功 其他:不支持的操作
 */
int IP_BDMA_Init(IP_BDMA* ip_bdma,BDMA_Channel_TypeDef* _bdma){

  ip_bdma->Instance.Instance=_bdma;

  if(ip_bdma->Instance.Instance->CR&BDMA_CCR_EN){
    ip_bdma->Instance.Instance->CR&=(~DMA_SxCR_EN);
    while(ip_bdma->Instance.Instance->CR&DMA_SxCR_EN){
    }
  }
  

  IP_BDMA_SetIF(ip_bdma);
  IP_BDMA_DisableAll_IT(ip_bdma);

  return 0;
}



